﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP.Common.Util.Interfaces;
using ERP.Common.Util.Util.Model;
using Microsoft.AspNetCore.Mvc;
using UERP.Common.Util.Model;

namespace ERP.Common.Util.Util.Controller;

public class BaseController<Entity> : ControllerBase 
    where Entity : class
{
    protected IBaseService<Entity> _baseService;

    public BaseController(IBaseService<Entity> baseService)
    {
        _baseService = baseService;
    }

    [HttpGet("GetAll")]
    public async Task<IActionResult> GetAll(int page = 1, int pageSize = 10)
    {
        PagedList<Entity> pagedList = await _baseService.GetAllAsync(page, pageSize);
        return Ok(RestResultBuilder<PagedList<Entity>>.Success(pagedList));
    }

    [HttpGet("GetById")]
    public async Task<IActionResult> GetById(int id)
    {
        Entity entity = await _baseService.GetByIdAsync(id);
        return Ok(RestResultBuilder<Entity>.Success(entity));
    }

    [HttpPost]
    public async Task<IActionResult> Create(Entity entity)
    {
        Entity tempEntity = await _baseService.CreateAsync(entity);
        return Ok(RestResultBuilder<Entity>.Success(tempEntity));
    }

    [HttpPut]
    public async Task<IActionResult> Update(Entity entity)
    {
        Entity tempEntity = await _baseService.UpdateAsync(entity);
        return Ok(RestResultBuilder<Entity>.Success(tempEntity));
    }

    [HttpDelete]
    public async Task<IActionResult> Delete(int id)
    {
        await _baseService.DeleteAsync(id);
        return Ok(RestResultBuilder<Entity>.Success());
    }

}
